# ---------------------------------------------------------------------------
# Author: Min Feng
# Date: 01/04/08
# creates sql file which can be run to synchronize the database.
# note: 
# 	command for running sql file: 
# 		psql [sql file name] -d dataview -U dataview
# 
#----------------------------------------------------------------------------


import configure

_conf = configure.configure()
_dir = _conf.getPathRepository()

import os
import datetime

_fileName = _conf.generateFileName('database.', '.sql')
print _fileName
_out = open(_fileName, 'w')
if __name__ == '__main__':
#    _in = open('d:\\mfeng\\data\\hshi\\txtdata\\list.txt', 'r')
    _out.write('delete from fire;\n')
    for _l in _conf.getDatasetList():
        _l = _l.strip()
        if len(_l) > 0:
##            if _l.startswith('\\radiance\\'):
            import re
            _m = re.match('.*(\w{3})(\d{2})(\d{3})(\d{3})(\d{2})(\d{2})(\d{4})00\.\w+', _l)

            if _m:
                _source = _m.group(1)
                _sensor = _m.group(2)
                _path = int(_m.group(3))
                _row = int(_m.group(4))
                _month = _m.group(5)
                _day = _m.group(6)
                _year = _m.group(7)
                _date = _year + '-' + _month + '-' + _day
                _note = 'radiance'
                _dir = _l

                _txt = 'insert into fire (path, row, date, sensor, source, dir, note) values (%(path)d,%(row)d,\'%(date)s\',\'%(sensor)s\',\'%(source)s\',E\'%(dir)s\',\'%(note)s\')' % {'path':_path, 'row':_row, 'date':_date, 'sensor':_sensor, 'source':_source,'dir':_dir,'note':_note}
                _out.write(_txt.replace('\\', '\\\\') + ';\n')
_out.close()
